How the type `Fix` and function `fix` are same in Haskell?
NickName:Ingun전인건 Ask DateTime:2020-01-28T23:50:56

How the type `Fix` and function `fix` are same in Haskell?

I'm trying to convince myself that type Fix and function fix are the same thing.
But I can't find the correlation between their definitions

-- definition of fix 
fix :: (p -> p) -> p
fix f = let {x = f x} in x -- or fix f = f (fix f)
-- definition of Fix
newtype Fix f = Fix { unFix :: f (Fix f) }

How does the constructor Fix fit into the form of (x -> x) -> x?

Copyright Notice:Content Author:「Ingun전인건」,Reproduced under the CC 4.0 BY-SA copyright license with a link to the original source and this disclaimer.
Link to original article:https://stackoverflow.com/questions/59952446/how-the-type-fix-and-function-fix-are-same-in-haskell

Answers
chepner 2020-01-28T15:55:36

Look at the kind of the type constructor Fix:\n\n> :k Fix\nFix :: (* -> *) -> *\n\n\nThe type constructor Fix is what's analogous to the function fix.\n\nThe data constructor is something else. Following the explanation found in Understanding F-Algebras, Fix is an evaluator: it evaluates a term of type f (Fix f) to produce a value of type Fix f. This evaluation is lossless; you can recover the original value from the result using unFix.",


More about “How the type `Fix` and function `fix` are same in Haskell?” related questions

How the type `Fix` and function `fix` are same in Haskell?

I'm trying to convince myself that type Fix and function fix are the same thing. But I can't find the correlation between their definitions -- definition of fix fix :: (p -> p) -> p fix f = ...

Show Detail

Haskell type and fix recursion examples

When reading about fix because I was interested in having recursive lambdas in my code I came upon this particular example of code (from Here): fix (\rec n -> if n == 0 then 1 else n * rec (n-1...

Show Detail

Haskell : how to get fix point of a function?

A fix-point of a function f is a value x such that f(x)=x . Write a function fix that takes a function f and returns its fix-point. For example: the pseudocode is as follows: f(x)= if (x=f(x)) r...

Show Detail

Need help understanding Fix type in Haskell

I didn't have any problems understanding how fix function works in Haskell, but I just can't wrap my head around Fix data type. I believe I get the idea behind it, but I just can't understand how Fix

Show Detail

Haskell: to fix or not to fix

I recently learned about Data.Function.fix, and now I want to apply it everywhere. For example, whenever I see a recursive function I want to "fix" it. So basically my question is where and when sh...

Show Detail

Haskell AST Annotation with Fix

I am working on creating an AST in Haskell. I want to add different annotations, such as types and location information, so I ended up using fixplate. However, I can't find any examples online and am

Show Detail

Understanding the Fix datatype in Haskell

In this article about the Free Monads in Haskell we are given a Toy datatype defined by: data Toy b next = Output b next | Bell next | Done Fix is defined as follows: data Fix f = Fix (f...

Show Detail

Haskell "fix" keyword failed on declaring a recursive lambda function

Seems a function should have a name to be recursive(in order to call itself), so how does a lambda function be recursive? I searched wikipedia and it says this could be done by a "Y-combinator". ...

Show Detail

FIx data type in OCaml

How can the following data type from Haskell be expressed in OCaml or SML? newtype Fix f = In (f (Fix f))

Show Detail

Haskell List function (map, zip, etc..) with fix

I try to learn haskell and have exercise -try to rewrite standart list operation(map, foldr, zip, iterate, etc.) with function fix. I have example with repeat: repeat a = fix $ \xs -> a : xs a...

Show Detail